package com.qd.system.service.dict;

import com.cdqidi.exception.ApiException;
import com.qd.common.sys.constant.Constants;
import com.qd.common.sys.enums.BaseTableEnum;
import com.qd.system.model.DictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 〈一句话功能简述〉<br>
 *
 * @author sjk
 * @since 1.0.0
 */
@Service
public class MbUtil {

    private MbUtil(){

    }
    private static DictDataService dictDataService = null;

    @Autowired
    public MbUtil(DictDataService dictDataService) {
        MbUtil.dictDataService = dictDataService;
    }

    private static final List<Map<String, Object>> LIST = new LinkedList<>();

    static {
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("id", Constants.STATUS_ENABLE);
        map.put("name", "启用");
        LIST.add(map);

        map = new LinkedHashMap<>(2);
        map.put("id", Constants.STATUS_DISABLE);
        map.put("name", "停用");
        LIST.add(map);

    }

    /**
     * 查询码表名字
     *
     * @param id 码表ID
     * @return 码表值
     */
    public static String idToDisplay(String id) {
        String display = "";
        if (StringUtils.hasLength(id)) {
            final DictData data = dictDataService.getById(id);
            display = (null == data ? "" : data.getName());
        }
        return display;
    }

    /**
     * 查询码表说明
     * @param id 码表id
     * @return 说明
     */
    public static String idToRemark(String id){
        String remark = "";
        if (StringUtils.hasLength(id)) {
            final DictData data = dictDataService.getById(id);
            remark = (null == data ? "" : data.getRemark());
        }
        return remark;
    }

    /**
     * 判断ID是否存在
     *
     * @param id            主键
     * @param baseTableEnum 类型
     * @return true or false
     */
    public static boolean isNotExists(String id, BaseTableEnum baseTableEnum) {
        try {
            final String typeId = baseTableEnum.getCode();
            if (!StringUtils.hasLength(id) || !StringUtils.hasLength(typeId)) {
                return true;
            }
            final List<DictData> listByType = dictDataService.getListByType(typeId);
            if (null == listByType) {
                return true;
            }
            final List<String> stringList = listByType.stream().map(DictData::getId).collect(Collectors.toList());
            final boolean b = stringList.contains(id);
            listByType.clear();
            stringList.clear();
            return !b;
        } catch (ApiException e) {
            return false;
        }
    }

    /**
     * 根据名字获得ID
     *
     * @param name   名字
     * @param typeId 类型
     * @return ID
     */
    public static String displayNameToId(String name, String typeId) {
        final List<DictData> list = dictDataService.getListByType(typeId);
        final Optional<DictData> optional = list.stream().filter(dictData -> dictData.getName().equals(name)).findFirst();
        return optional.orElse(new DictData()).getId();
    }


    public static String idToDisplay(Integer status) {
        if (null == status) {
            return "";
        }
        if (status == Constants.STATUS_ENABLE) {
            return Constants.STATUS_ENABLE_VALUE;
        } else if (status == Constants.STATUS_DISABLE) {
            return Constants.STATUS_DISABLE_VALUE;
        }
        return "";
    }

    public static Integer displayToId(String value) {
        if (Constants.STATUS_ENABLE_VALUE.equals(value)) {
            return 0;
        } else if (Constants.STATUS_DISABLE_VALUE.equals(value)) {
            return 1;
        }
        return null;
    }

    public static Integer isStatusValidate(List<String> errors, String statusDisplay) {
        if (StringUtils.hasLength(statusDisplay)) {
            final Integer status = MbUtil.displayToId(statusDisplay);
            if (null == status) {
                errors.add("状态值错误");
                return null;
            }
            return status;
        }
        return null;
    }

    public static boolean isExists(int status) {
        return status == Constants.STATUS_ENABLE || status == Constants.STATUS_DISABLE;
    }

    public static List<Map<String, Object>> getList() {
        return LIST;
    }

    public static <T> boolean isExistInCollection(Collection<T> ids, T t) {
        if (null == ids || ids.isEmpty()) {
            return false;
        }
        if (null != t) {
            return ids.contains(t);
        }
        return false;
    }

}